#ifndef __ODBC_DATABASE_H__
  #define __ODBC_DATABASE_H__

#include "database.h"

#include <windows.h>
#include <C:/PKH/MinGW64/x86_64-w64-mingw32/include/sql.h>
#include <sqlext.h> // ODBC include file

using namespace std;

#define ODBC_BUFFER_SIZE (256 * 1024)

//! Wrapper for ODBC database

/*!
This class is inherited from database and offers access to ODBC database system(s).

Currently not supported feature(s):
\li binary-data in columns is not supported
\li maximum length of each column is limited to the ODBC_BUFFER_SIZE (256KB)
\li lock_table & unlock_table are not supported

Implementation date: 2008

Usage:

\code
record_set rs;
odbc_database od;
od.connect("DSN=PSQL;", "protocol_file.txt");
od.execute_query("SELECT * FROM \"T1\" LIMIT 10;", rs);
cout << rs << endl;
\endcode
*/

class odbc_database : public database {
private:
  wchar_t buf[ODBC_BUFFER_SIZE]; // a member variable, because of performance tuning
  mutable string str_cnv; // a member variable, because of performance tuning

  SQLHENV env;
  SQLHDBC dbc;

  string get_error_details(SQLHANDLE handle, SQLSMALLINT type) const;
  void stop_transaction();

   //! fills the record_set with data
  void process_columns(SQLHSTMT &stmt_handle, const unsigned int columns, record_set &rs);

public:
  virtual void connect(const string &connection_string, const string &protocol_file = global_empty_string);
  virtual void connect(const string &host, const string &user_id, const string &password, const string &db_name, const string &protocol_file = global_empty_string);
  virtual void disconnect();

  virtual bool is_connected();

  virtual void execute_query(const string &s, record_set &rs, bool append_to_record_set = false);
  virtual void execute_update(const string &s);

  virtual void start_transaction();
  virtual void commit();
  virtual void rollback();

  //! this method does nothing. not supported by this odbc implementation
  virtual void lock_table(const string &table_name);

  //! this method does nothing. not supported by this odbc implementation
  virtual void unlock_table(const string &table_name);

  //! default constructor
  odbc_database();

  //! destructor
  virtual ~odbc_database();
}; // class odbc_database

#endif
